package igame.poker.serverside.core;

import igame.poker.serverside.event.*;

import java.io.*;
import java.net.*;
import java.util.*;

/**
 * 客户端
 * 
 * @author AfritXia
 *
 */
public class ClientProxy implements Runnable {
	/**
	 * 客户端 Socket
	 * 
	 */
	private Socket _clientSocket = null;

	/**
	 * 输出流
	 * 
	 */
	private PrintWriter _pw = null;

	/**
	 * 输入流
	 * 
	 */
	private BufferedReader _br = null;

	/**
	 * 数据事件监听数组
	 * 
	 */
	private ArrayList<IDataEventListener> _dataEventListenerArray = null;
	
	/**
	 * 类参数构造器
	 * 
	 * @param clientSocket 
	 * 
	 */
	public ClientProxy(Socket clientSocket) {
		if (clientSocket == null) {
			throw new RuntimeException(new IllegalAccessException("参数为空"));
		}

		this._clientSocket = clientSocket;
	}

	/**
	 * 添加数据事件监听器
	 * 
	 * @param l
	 * 
	 */
	public void addDataEventListener(IDataEventListener l) {
		if (l == null) {
			return;
		}

		if (this._dataEventListenerArray == null) {
			this._dataEventListenerArray = new ArrayList<IDataEventListener>();
		}

		this._dataEventListenerArray.add(l);
	}

	/**
	 * 移除数据事件监听器
	 * 
	 * @param l 
	 * 
	 */
	public void removeDataEventListener(IDataEventListener l) {
		if (l == null) {
			return;
		}

		if (this._dataEventListenerArray == null) {
			return;
		}

		this._dataEventListenerArray.remove(l);
	}

	/**
	 * 清除数据事件监听器
	 * 
	 */
	public void clearDataEventListener() {
		if (this._dataEventListenerArray == null) {
			return;
		}

		this._dataEventListenerArray.clear();
	}

	/**
	 * 接收字符串
	 * 
	 * @param value 
	 * 
	 */
	public void receiveString(String value) {
		if (value == null) {
			return;
		}

		// 输出字符串
		this.getPrintWriter().println(value);
		this.getPrintWriter().flush();
	}

	/**
	 * 获取发送过来的字符串
	 * 
	 * @return 
	 * 
	 */
	public String getPostedString() {
		try {
			// 获取读入行
			return this.getBufferedReader().readLine();
		} catch (Exception ex) {
			// 抛出异常
			throw new RuntimeException(ex);
		}
	}

	/**
	 * 获取客户端 Socket 
	 * 
	 * @return 
	 * 
	 */
	private Socket getClientSocket() {
		return this._clientSocket;
	}

	/**
	 * 获取输出流
	 * 
	 * @return 
	 * 
	 */
	private PrintWriter getPrintWriter() {
		if (this._pw == null) {
			try {
				// 创建输出流
				this._pw = new PrintWriter(this.getClientSocket().getOutputStream(), true);
			} catch (Exception ex) {
				// 抛出异常
				throw new RuntimeException(ex);
			}
		}

		return this._pw;
	}

	/**
	 * 获取输入流
	 * 
	 * @return 
	 * 
	 */
	private BufferedReader getBufferedReader() {
		if (this._br == null) {
			try {
				this._br = new BufferedReader(new InputStreamReader(this.getClientSocket().getInputStream(), "utf8"));
			} catch (Exception ex) {
				// 抛出异常
				throw new RuntimeException(ex);
			}
		}

		return this._br;
	}

	@Override
	public void run() {
		while (true) {
			// 获取发送过来的字符串
			String postedString = this.getPostedString();

			if (postedString != null) {
				// 打印调试数据
				System.out.println(String.format("DEBUG: postedString = %s", postedString));

				// 创建数据事件
				DataEvent event = new DataEvent(this, postedString);
				
				// 触发数据事件
				this.raiseDataEvent(event);
			}
		}
	}

	/**
	 * 触发数据事件
	 * 
	 * @param event 
	 * 
	 */
	protected void raiseDataEvent(DataEvent event) {
		if (event == null) {
			throw new RuntimeException(new IllegalArgumentException("参数为空"));
		}

		for (IDataEventListener l : this._dataEventListenerArray) {
			l.handle(event);
		}
	}
}
